﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vanfa.Entities.User;
using Vanfa.Queries.Sql.DbQueries;
using Vanfa.Queries.Sql.DbModel;
using Vanfa.Common.Crypto;

namespace Vanfa.Biz.User
{
    public class UserMag
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="lg"></param>
        /// <returns></returns>
        public static RetUserLogin Login(vLogin lg)
        {
            var ret = UserQry.New.Select(lg.UserName);
            if (ret == null)
                return RetUserLogin.User_Not_Exists;
            if (ret.Password != lg.Md5Pwd)
                return RetUserLogin.User_Pwd_Not_Match;
            UserQry.New.Insert(
                new userLoginHistory
                {
                    UserID = ret.UserID,
                    LoginTime = DateTime.Now
                });
            return RetUserLogin.Success;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="rg"></param>
        /// <returns></returns>
        public static bool Regist(vRegist rg)
        {
            int userID = UserQry.New.Insert(rg.ToDbEntity());
            rg.UserID = userID;
            return userID > 0;
        }

        /// <summary>
        /// 检测用户名
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static RetCheckUnique CheckUserName(string userName)
        {
            var ret = UserQry.New.Select(userName);
            if (ret != null)
                return RetCheckUnique.User_Exists;
            return RetCheckUnique.Can;
        }

        /// <summary>
        /// 检测用户手机号
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static RetCheckUnique CheckMobile(string mobile)
        {
            var ret = UserQry.New.Select(mobile, 2);
            if (ret != null)
                return RetCheckUnique.Mobile_Exists;
            return RetCheckUnique.Can;
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public static string CreateCode(string mobile)
        {
            var code = Crypto.Rnd(6);
            UserQry.New.Insert(mobile, code);
            return code;
        }

        /// <summary>
        /// 检查验证码
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool CheckUserCode(string mobile, string code)
        {
            var bc = UserQry.New.SelectCode(mobile).ToBizEntity();
            if (bc == null)
                return false;
            if (bc.Code != code)
                return false;
            if (bc.IsExpire)
                return false;
            return true;
        }

        public static vUserTicket GetUserTicket(string userName)
        {
            var um = UserQry.New.Select(userName);
            vUserTicket vt = new vUserTicket { 
                UserID = um.UserID,
                UserName = um.UserName,
                Mobile = um.Mobile,
                UserType = um.UserType
            };
            return vt;
        }
    }
}
